<template>
	<view class="full-screen">
		<view class="full_box top" @click="chooseRole('big')">
			<view class="">
				我是<text class="">工厂人事部/源头大劳务</text>
			</view>
			<view class="">
				我要使用本小程序做招工管理
			</view>
			<view class="">
				&
			</view>
			<view class="">
				我要借助社会力量帮我代招工
			</view>
		</view>
		<view class="full_box center" @click="chooseRole('small')">
			<view class="">
				我是<text class="">一线劳务工作者</text>
			</view>
			<view class="">
				我想帮别人招工来赚钱
			</view>
		</view>
		<view class="bottom" v-if="token">
			<u-button :custom-style="customStyle" type="info" @click="closeData('any')">退出登录</u-button>
		</view>
		<!--弹框 -->
		<u-popup v-model="showBigLoginIn" mode="center" border-radius="10" width="600">
			<view class="popup">
				<view class="t-text">
					先添写手机号进行注册吧！
				</view>
				<view class="">
					<u-field v-model="uForm.phone" label="手机号" placeholder="请填写手机号" required :error-message="phoneErr"></u-field>
					<u-field v-model="uForm.code" label="验证码" placeholder="请填写验证码" required :error-message="codeErr">
						<u-button size="mini" slot="right" type="success" @click="getCode">{{uForm.codeText}}</u-button>
					</u-field>
					<u-verification-code ref="uCode" @change="codeChange"></u-verification-code>
				</view>
				<u-button @click="loginBigIn" size="medium" type="primary" :custom-style="customStyle">提交</u-button>
			</view>
			
		</u-popup>
	</view>
</template>

<script>
import { mapState } from 'vuex';
const obj = {
	0: {
		lable: '工人',
		value: 'any'
	},
	1: {
		label: '普通小中介',
		value: 'small'
	},
	2: {
		label: '专属小中介',
		value: 'small'
	},
	3: {
		label: '大中介',
		value: 'big'
	}
}
export default {
	components: {
		
	},
	data() {
		return {
			userId: '',
			customStyle: {
				fontSize: '20px',
				marginTop: '40rpx',
				width: '100%'
			},
			codeErr: ' ',
			phoneErr: ' ',
			recId: '',
			token: uni.getStorageSync('token'),
			showBigLoginIn: false,
			uForm: {
				phone: null,
				code: null,
				codeText: '获取验证码'
			},
		};
	},
	onLoad(e = {}) {
		if(e.recId && e.recId != 'undefined' ) this.recId = e.recId
		// if(uni.getStorageSync('role') && (uni.getStorageSync('role') == 'small' || uni.getStorageSync('role') == 'big')) {
		// 	uni.reLaunch({
		// 			url: '/pages/index/home'
		// 		})
		// }
	},
	methods: {
		codeChange(text) {
			this.uForm.codeText = text;
		},
		getCode() {
			if(this.$refs.uCode.canGetCode) {
				if(!this.uForm.phone) {
					this.phoneErr = '请先填写手机号'
					return
				}
				// 向后端请求验证码
				this.$u.api.bigSendSms({phone: this.uForm.phone}).then(res => {
					if (res.status == true) {
						this.$refs.uCode.start();
					} else {
						uni.showToast({
							title: res.msg,
							duration: 2000,
							icon: 'none'
						});
					} 
				})
			}else {
				this.$u.toast('倒计时结束后再发送');
			}
		},
		loginBigIn() {
			if(!this.uForm.phone) {
				this.phoneErr = '请先填写手机号'
				return
			}
			if(!this.uForm.code) {
				this.codeErr = '请先填写验证码'
				return
			}
			this.postLoginCheck()
			// this.$u.api.bigLogin().then(res => {
			// 	if (res.status == true) {
			// 		// this.$refs.uCode.start();
			// 	} else {
			// 		uni.showToast({
			// 			title: res.msg,
			// 			duration: 2000,
			// 			icon: 'none'
			// 		});
			// 	} 
			// })
			
		},
		postLoginCheck() {
			let params = {
				...this.uForm
			}
			this.$u.api
				.bigLogin(params)
				.then(res => {
					if (res.status == true) {
						console.log(res, '[登录页面-登录]');
						uni.setStorageSync('token', res.data)
						const typeItem = obj[res.type]
						// 获取用户信息
						this.postGetAdminInfo().then((res) => {
							this.chooseRole(typeItem.value)
							console.log(res)
							// uni.navigateBack()
							// this.submit()
							// uni.reLaunch({
							// 	url: './index/home'
							// })
						})
					}
				})
				.catch(err => {
					console.log(err);
				});
		},
		// 获取用户信息
		postGetAdminInfo() {
			return new Promise((resolve, reject) => {
				this.$u.api
					.postGetAdminInfo()
					.then(res => {
						if (res.status == true) {
							uni.setStorageSync('userPhone', res.data.phone);
							// this.$store.commit('commitUserId', res.data.id);
							uni.setStorageSync('userId', res.data.id);
							uni.setStorageSync('userInfo', res.data)
							this.$store.commit('commitUserInfo', res.data)
							switch(res.data.type) {
								case 0:
								 uni.setStorageSync('role', 'any')
								 break
								case 1 || 2:
								uni.setStorageSync('role', 'small')
								break
								case 3:
								uni.setStorageSync('role', 'big')
							}
						}
						resolve();
					})
					.catch(err => {
						console.log(err);
						reject();
					});
			})
		},
		closeData() {
			try {
			    uni.clearStorageSync();
				uni.showToast({
					title: '操作成功'
				})
			} catch (e) {
				uni.showToast({
					title: '操作失败！',
					icon: 'none'
				})
			}
			  
		},
		loginBig() {
			
		},
		chooseRole(type) {
			const userInfo = uni.getStorageSync('userInfo')
			if(type == 'any') {
				uni.setStorageSync('role',type)
				uni.reLaunch({
						url: '/pages/index/home'
					})
				return
			}
			
			// 没有登录
			if(!userInfo) {
				
				uni.showModal({   
					title: '提示',
					content: '您还没有登录，是否现在去登录？',
					cancelText: '先看看',
					confirmText: '去登录',
					success: res => {
						if(res.confirm) {
							if(type == 'big') {
								this.showBigLoginIn = true
								return
							} else {
								uni.navigateTo({
									url: '/pages/login'
								})
							}
						} else {
							uni.reLaunch({
								url: '/pages/index/home'
							})
						}
					}
				})
				return
			}
			
			//  没有身份 
			if(!userInfo.type) {
				switch(type) {
					case 'big':
						this.submit()
						break
						// uni.navigateTo({
						// 	url: '/pages/share/toApply'
						// })
					case 'small':
						uni.navigateTo({
							url: '/pages/share/invitation?recId=' + this.recId
						})
						break
					default:
						uni.reLaunch({
							url: '/pages/index/home'
						})
				}
				return
			}
			if(obj[userInfo.type].value == type) {
				uni.setStorageSync('role',type)
				uni.reLaunch({
					url: '/pages/index/home'
				})
				
			} else {
				console.log('==》', type)
				switch(type) {
					case 'big':
					 //  	uni.navigateTo({
						// 	url: '/pages/share/toApply'
						// })
						this.submit()
						return
					case 'small':
						uni.navigateTo({
							url: '/pages/share/invitation?recId=' + this.recId
						})
						return
					default:
						uni.reLaunch({
							url: '/pages/index/home'
						})
				}
			}
		},
		submit() {
			this.$u.api.applyBig().then(res => {
				if (res.status == true) {
					uni.setStorageSync('role','big')
					uni.reLaunch({
						url: '/pages/index/home'
					})
					// uni.showToast({
					// 	title: '申请成功！',
					// 	duration: 2000
					// });
					
				} else {
					uni.showToast({
						title: res.msg,
						duration: 2000,
						icon: 'none'
					});
				} 
			})
		},
	}
};
</script>

<style lang="scss" scoped>
	.full-screen{
		width: 670upx;
		margin: 60upx auto;
		font-size: 40upx;
		text-align: center;
        padding-top: calc(var(--status-bar-height) + 20px);
	}
	.full_box{
		padding: 30upx 30upx 60upx;
		border: 1px solid #C0C0C0;
		background-color: #FFFFFF;
		&.center{
			margin-top: 20upx;
		}
	}
	.bottom{
		margin-top: 60upx;
		font-size: 30upx;
	}
	.popup{
		.t-text{
			font-size: 30rpx;
			line-height: 80rpx;
			padding: 0 10rpx;
		}
		.form-login{
			padding:0 20rpx;
		}
		.form-input{
			display: flex;
		}
	}
</style>
